/*!
 * \file merge_symbol_table_visitor.cc
 *
 * \brief Implementations of the merge_symbol_table_visitor that traverses the
 *        Symbol Table's symbols
 *
 * \date Started: 9/13/2010
 * \date Last Modified: 9/12/2010
 * \authors Ian Craig
 *
 * Copyright (c) 2010, Colorado State University <br>
 * All rights reserved. <br>
 * See ../../COPYING for details. <br>
 */

#include <iegenlib/iegenlib.h>

namespace iegenlib{

MergeSymbolTableVisitor::~MergeSymbolTableVisitor(){
  if(currExpVec != NULL){
    delete currExpVec;
  }
}

void MergeSymbolTableVisitor::ExpVecIn(const ExpVec* vec){
  ExpVec copy(*vec);

  if(currExpVec!=NULL){
    delete currExpVec;
  }

  currExpVec = new ExpVec(oldST->convertExpVec(copy, newST, mapping));
}

void MergeSymbolTableVisitor::UFCallCase(UFCall* ufcall){
  list<ExpVec> newList;
  for(list<ExpVec>::const_iterator itr = ufcall->getParamsBegin(); itr != ufcall->getParamsEnd(); ++itr){
    newList.push_back(ExpVec(oldST->convertExpVec(*itr, newST, mapping)));
  }
  newST->lookup(ufcall->getId(), newList);	
}

void MergeSymbolTableVisitor::SREqualityOut(const SREquality* eq){
  equals.insert(SREquality(*currExpVec));
}

void MergeSymbolTableVisitor::SRInequalityOut(const SRInequality* eq){
  inequals.insert(SRInequality(*currExpVec));
}

void MergeSymbolTableVisitor::SRConjunctionOut(const SRConjunction* SRConj){
  //Go through this particular conjunction's tuplelist and replace the variables with the input tuplelist
  list<TupleType> newTuples;
  for(list<TupleType>::const_iterator itr = SRConj->tuplevarsBegin(), oitr = this->newTups.begin(); itr != SRConj->tuplevarsEnd() && oitr != this->newTups.end(); ++itr, ++oitr){
    //If this tuple location isn't const add the new name to the list
    if(!itr->isConst()){
      newTuples.push_back(TupleType(oitr->getName()));
    //else add the old constant
    }else{
      newTuples.push_back(TupleType(itr->getValue()));
    }
  }
  //create a new SRConjunction and add it to the current set
  conjunctions.insert(SRConjunction(equals, inequals, newTuples));
  //clear the old equality and inequality sets to prep them to go again
  equals.clear();
  inequals.clear();
}

//determine if a tupleVar in the old sym-table exists in the new sym-table using the mapping
int MergeSymbolTableVisitor::is_tupleVar(ExpVec vec, int i){
    STE* thisSTE = oldST->lookup(TUPLE, i);
    string newName = (*mapping)[thisSTE->getId()];
    if(newName == ""){
       return 111;
    }else{
       return 222;
    }
  }

}//end namespace iegenlib
